package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TBDevice;
import com.ruoyi.system.domain.TBOrderDevice;
import com.ruoyi.system.service.ITBDeviceService;
import com.ruoyi.system.service.ITBOrderDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备管理Controller
 * 
 * @author pspyy
 * @date 2022-03-07
 */
@RestController
@RequestMapping("/system/device")
@Api("设备管理")
public class TBDeviceController extends BaseController
{
    @Autowired
    private ITBDeviceService tBDeviceService;

    @Autowired
    private ITBOrderDeviceService tBOrderDeviceService;
    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/list")
    @ApiOperation("查询设备管理列表")
    public TableDataInfo list(TBDevice tBDevice)
    {
        startPage();
        List<TBDevice> list = tBDeviceService.selectTBDeviceList(tBDevice);
        return getDataTable(list);
    }

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/listById")
    @ApiOperation("查询设备管理列表")
    public TableDataInfo listById(TBDevice tBDevice)
    {
        startPage();
        List<TBDevice> list = tBDeviceService.selectTBDeviceListById(tBDevice);
        return getDataTable(list);
    }
    /**
     * 根据ids查查询数据
     */
    @GetMapping("/deviceList/{deviceIds}")
    @ApiOperation("根据ids查查询数据")
    public List<TBDevice> list(@PathVariable String[] deviceIds)
    {
        List<TBDevice> list = tBDeviceService.selectDeviceListByIds(deviceIds);
        return list;
    }
    @Log(title = "设备管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:device:import')")
    @PostMapping("/importData")
    @ApiOperation("设备导入")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TBDevice> util = new ExcelUtil<TBDevice>(TBDevice.class);
        List<TBDevice> deviceList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tBDeviceService.importCustomer(deviceList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    @ApiOperation("设备数据导入")
    public void importTemplate(HttpServletResponse response)
    {
        HSSFWorkbook wb = new HSSFWorkbook();
        ExcelUtil<TBDevice> util = new ExcelUtil<TBDevice>(TBDevice.class);
        util.importTemplateExcel(response, "设备数据");
    }
    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出设备管理列表")
    public void export(HttpServletResponse response, TBDevice tBDevice)
    {
        List<TBDevice> list = tBDeviceService.selectTBDeviceList(tBDevice);
        ExcelUtil<TBDevice> util = new ExcelUtil<TBDevice>(TBDevice.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取设备管理详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tBDeviceService.selectTBDeviceById(id));
    }

    /**
     * 新增设备管理
     */
    @PreAuthorize("@ss.hasPermi('system:device:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增设备管理")
    public AjaxResult add(@RequestBody TBDevice tBDevice)
    {
        if("0".equals(tBDeviceService.checkDeviceNameUnique(tBDevice.getDeviceName()))){
            return AjaxResult.error("新增设备'" + tBDevice.getDeviceName()+ "'失败，设备已存在");
        }
        return toAjax(tBDeviceService.insertTBDevice(tBDevice));
    }

    /**
     * 修改设备管理
     */
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改设备管理")
    public AjaxResult edit(@RequestBody TBDevice tBDevice)
    {
        return toAjax(tBDeviceService.updateTBDevice(tBDevice));
    }

    /**
     * 删除设备管理
     */
    @PreAuthorize("@ss.hasPermi('system:device:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除设备管理")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<TBOrderDevice> tbOrderDevices = tBOrderDeviceService.selectByDeviceId(ids);
        if(tbOrderDevices.size() > 0){
            return AjaxResult.error("设备已存在关联订单，无法删除！");
        }
        return toAjax(tBDeviceService.deleteTBDeviceByIds(ids));
    }
}
